

#include "public.h"


u16 calculate_crc16(u8 *data, u16 length, u16 initial_value, u16 polynomial) 
{
    u16 crc = initial_value;
    u16 i, j;

    for (i = 0; i < length; i++) 
    {
        crc ^= (u16)data[i];
        for (j = 0; j < 8; j++) 
        {
            if (crc & 0x0001) 
            {
                crc = (crc >> 1) ^ polynomial;
            } else 
            {
                crc = crc >> 1;
            }
        }
    }

    return crc;
}








